package com.affectu.user.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;

import com.affectu.dao.DAOBase;
import com.affectu.user.Role;
import com.affectu.user.User;

/**
 * Oct 16, 2008
 * 
 * @author daniel nathan
 */
@Name("roleAction")
public class RoleAction extends DAOBase {

	public String addRole(Role role) {
		try {
			role.setCreateDate(new Date());
			em.persist(role);
		} catch (Exception ex) {
			this.logAffectuException(ex, "");
		}
		return "";
	}

	public String saveRole(User user) {
		try {
			em.merge(user);
			em.flush();
		} catch (Exception ex) {
			this.logAffectuException(ex, "");
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	@Factory("roleAction_getRoles")
	public List<Role> getRoles() {
		List<Role> roles = new ArrayList<Role>();
		try {
			roles = em.createQuery("from Role r order by r.createDate desc")
					.list();
		} catch (Exception ex) {
			this.logAffectuException(ex, "");
		}
		return roles;
	}
}
